package org.icop.model;

import java.util.HashSet;
import java.util.Set;

import org.jbpt.hypergraph.abs.IVertex;

/**
 * Class that represents a correspondences between two vertices.
 * 
 * @author matthias.weidlich
 *
 * @param <V> template for the type of vertices considered
 */
public class Correspondence<V extends IVertex> {
	
	private Set<V> v1s = null;
	
	private Set<V> v2s = null;

	public Correspondence(Set<V> v1s, Set<V> v2s) {
		this.v1s = v1s;
		this.v2s = v2s;
	}

	public Correspondence(V v1, V v2) {
		this.v1s = new HashSet<V>();
		this.v2s = new HashSet<V>();
		this.v1s.add(v1);
		this.v2s.add(v2);
	}
	
	public V getV1() {
		return v1s.iterator().next();
	}


	public V getV2() {
		return v2s.iterator().next();
	}

	public Set<V> getV1s() {
		return v1s;
	}

	public Set<V> getV2s() {
		return v2s;
	}
	
	public Set<Correspondence<V>> expand() {
		Set<Correspondence<V>> result = new HashSet<>();
		for (V v1 : v1s)
			for (V v2 : v2s)
				result.add(new Correspondence<V>(v1, v2));
		
		return result;
	}

	@Override
	public String toString() {
		return this.v1s.toString() +  " - " +  this.v2s.toString();
	}

	@Override
	public boolean equals(Object c) {
		if (!(c instanceof Correspondence<?>))
			return false;
		Correspondence<?> cc = (Correspondence<?>)c;
		return this.v1s.equals(cc.getV1s()) && this.v2s.equals(cc.getV2s());
	}

	@Override
	public int hashCode () {
		int hash = 17;
		hash *= 11 + getV1s().hashCode();
		hash *= 11 + getV2s().hashCode();
		return hash;
	}

}
